package com.itheima.dubbo.mysql.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.commons.model.domain.Question;
import com.itheima.dubbo.interfaces.QuestionApi;
import com.itheima.dubbo.mysql.mapper.QuestionMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

@Service
public class QuestionApiImpl implements QuestionApi {

    @Autowired
    private QuestionMapper questionMapper;


    @Override
    public Question findQuestionByUserId(Long userId) {
        Question question = questionMapper.selectOne(Wrappers.lambdaQuery(Question.class)
                .eq(Question::getUserId, userId));
        return question;
    }

    @Override
    public void saveQuestions(Long userId, String content) {
        //查询陌生人问题
        Question question = findQuestionByUserId(userId);
        if (ObjectUtil.isNotEmpty(question)) {
            question.setTxt(content);
            question.setUpdated(new Date());
            this.questionMapper.updateById(question);
        } else {
            question = new Question();
            question.setTxt(content);
            question.setUserId(userId);
            question.setCreated(new Date());
            question.setUpdated(question.getCreated());
            this.questionMapper.insert(question);
        }
    }
}
